/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.SABM;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import entity.*;
import java.util.ArrayList;
import session.TX.*;
import managedBean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedProperty;
import managedBean.TX.OutletManagement.TrackerManagedOutletMB;

/**
 *
 * @author Randall Chia
 */
@ManagedBean(name = "rewardMB")
@SessionScoped
public class RewardMB {
    
    @EJB
    private ULPSBRemote pointsManager;
    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    
    @ManagedProperty(value = "#{TrackerOutletAdmin}")
    private TrackerManagedOutletMB trackerManagedOutletMB;
    
    
    //data fields for making a new reward item
    private String stakeholder;
    private String customerEmail;
    private String reward_Name;
    private String reward_Description;
    private Integer reward_Cost;         //the number of points needed to redeem this reward
    private Integer vouchers_left;       //the maximum number of times this reward can be redeemed. 
    private boolean unlimitedVouchers;
    
    
    

    /** Creates a new instance of RewardMB */
    public RewardMB() {
    }
     
    //STAKEHOLDER-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardItem> getStakeholderItems(){
      stakeholder = getPortalManagedBean().getStakeholderName();
      return getPointsManager().getRewardsByStakeholder(stakeholder);
    } 
    
    public void deleteReward(RewardItem itemToDelete){
        System.out.println(">>> Deleting a reward item");
        getPointsManager().stakeholderDeleteRewardItem(itemToDelete.getId());
        System.out.println(">>> Done.");
    }
    
    public void createReward(){
        System.out.println(">>> Creating a reward item");
        stakeholder = getPortalManagedBean().getStakeholderName();
        
        getPointsManager().stakeholderCreateRewardItem(stakeholder, reward_Name, reward_Description, reward_Cost, vouchers_left, unlimitedVouchers);
        System.out.println(">>> Done.");
        reward_Name = null;
        reward_Description = null;
        reward_Cost = null;
        vouchers_left = null;
    }
    //=========================================================================================================================
    
    
    //OUTLET-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardVoucher> getCustomerVouchers(){
        List<RewardVoucher> returnList = new ArrayList<RewardVoucher>();
        stakeholder = trackerManagedOutletMB.getOutletOwner();
        
        //Outlet managers can only get vouchers of rewards from their stakeholder. Only non-redeemed vouchers are shown.
        for(RewardVoucher voucher : pointsManager.getVouchersByCustomerAndStakeholder(customerEmail, stakeholder)){
            if(voucher.isRedeemed() == false) returnList.add(voucher);
        }
        
        return returnList;
        
    }
    
    public void redeemVoucher(long voucherID){
        System.out.println("MANAGED BEAN: Redeeming voucher...");
        pointsManager.redeemVoucher(voucherID);
        System.out.println("MANAGED BEAN: Finished...");
    }
    //=========================================================================================================================
    
    
    //USER-LEVEL FUNCTIONS
    //=========================================================================================================================
    public List<RewardVoucher> getVouchersByCustomer(){
        List<RewardVoucher> returnList = new ArrayList<RewardVoucher>();
        
        long customerID = getPortalManagedBean().getCustomerID();
        
        for(RewardVoucher voucher : pointsManager.getVouchersByCustomer(customerID)){
            returnList.add(voucher);
        }
        
        return returnList;
        
    }
    
    

    /**
     * @return the pointsManager
     */
    public ULPSBRemote getPointsManager() {
        return pointsManager;
    }

    /**
     * @param pointsManager the pointsManager to set
     */
    public void setPointsManager(ULPSBRemote pointsManager) {
        this.pointsManager = pointsManager;
    }

    /**
     * @return the portalManagedBean
     */
    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    /**
     * @param portalManagedBean the portalManagedBean to set
     */
    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    /**
     * @return the stakeholder
     */
    public String getStakeholder() {
        return stakeholder;
    }

    /**
     * @param stakeholder the stakeholder to set
     */
    public void setStakeholder(String stakeholder) {
        this.stakeholder = stakeholder;
    }

    /**
     * @return the reward_Name
     */
    public String getReward_Name() {
        return reward_Name;
    }

    /**
     * @param reward_Name the reward_Name to set
     */
    public void setReward_Name(String reward_Name) {
        this.reward_Name = reward_Name;
    }

    /**
     * @return the reward_Description
     */
    public String getReward_Description() {
        return reward_Description;
    }

    /**
     * @param reward_Description the reward_Description to set
     */
    public void setReward_Description(String reward_Description) {
        this.reward_Description = reward_Description;
    }

    /**
     * @return the reward_Cost
     */
    public Integer getReward_Cost() {
        return reward_Cost;
    }

    /**
     * @param reward_Cost the reward_Cost to set
     */
    public void setReward_Cost(Integer reward_Cost) {
        this.reward_Cost = reward_Cost;
    }

    /**
     * @return the vouchers_left
     */
    public Integer getVouchers_left() {
        return vouchers_left;
    }

    /**
     * @param vouchers_left the vouchers_left to set
     */
    public void setVouchers_left(Integer vouchers_left) {
        this.vouchers_left = vouchers_left;
    }

    /**
     * @return the unlimitedVouchers
     */
    public boolean isUnlimitedVouchers() {
        return unlimitedVouchers;
    }

    /**
     * @param unlimitedVouchers the unlimitedVouchers to set
     */
    public void setUnlimitedVouchers(boolean unlimitedVouchers) {
        this.unlimitedVouchers = unlimitedVouchers;
    }

    /**
     * @return the customer
     */
    public String getCustomerEmail() {
        return customerEmail;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    /**
     * @return the trackerManagedOutletMB
     */
    public TrackerManagedOutletMB getTrackerManagedOutletMB() {
        return trackerManagedOutletMB;
    }

    /**
     * @param trackerManagedOutletMB the trackerManagedOutletMB to set
     */
    public void setTrackerManagedOutletMB(TrackerManagedOutletMB trackerManagedOutletMB) {
        this.trackerManagedOutletMB = trackerManagedOutletMB;
    }
    
    
    
    
    
    
    
    
    
    
    
    
}
